package milu.models;

import com.google.appengine.api.datastore.Key;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Embedded;
import javax.jdo.PersistenceManager;

import com.google.appengine.api.datastore.Blob;

import com.google.appengine.api.users.*;
import javax.jdo.Query;

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import milu.*;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class MyUser {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    @Persistent
    private Date created;
    @Persistent
    private String name;
    @Persistent
    private String email;
    @Persistent
    private String password; // encode this
    @Persistent
    private Key avatarKey; // regular 128x128 avatar - links to MyImage

    public MyUser(String name, String email, String password) {
        this.created = new Date();
        this.name = name;
        this.email = email;
        this.password = password;
        this.avatarKey = null;
    }
    public Key getKey() {
        return key;
    }
    public Key getAvatarKey() {
        return avatarKey;
    }
    public void setAvatarKey(Key avatarKey) {
        this.avatarKey = avatarKey;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public MyImage getAvatar() {
        if(this.avatarKey == null) return null;
        PersistenceManager pm = Datastore.getPersistenceManager();
        MyImage myImage = pm.getObjectById(MyImage.class, this.avatarKey);
        return myImage;
    }

    /**
     * Gets a user object based on Key that is detached
     * so it can be edited later without refetching it
     */
    public static MyUser getUserInfo(Key userKey) {
        PersistenceManager pm = Datastore.getPersistenceManager();
        MyUser myUser = pm.getObjectById(MyUser.class, userKey);
        return myUser;
    }
    /**
     * we only store the userKey of the MyUser object in the session
     * so if the key doesn't exist, we're not logged in. simple!
     */
    public static MyUser getCurrentUser(HttpSession session) {
        Key userKey = (Key)session.getAttribute("userKey");

        if(userKey == null) return null;
        return getUserInfo(userKey);
        /*
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser(); // or req.getUserPrincipal()
        if(user == null) return null;
        return getUserInfo(user.getUserId());
        */
    }
    public static boolean userExists(String email) {
        PersistenceManager pm = Datastore.getPersistenceManager();

        Query query = pm.newQuery(MyUser.class, "this.email == email");
        query.declareParameters("String email");
        List<Recipe> found = (List<Recipe>)query.execute(email);
        if(found.size() == 0) {
            return false;
        } else {
            return true;
        }
    }
}
